var MongoClient = require('mongodb').MongoClient;

class Dbs {
    MongoClient:any;
    url:string;
    constructor(){
        this.MongoClient = MongoClient
        this.url = 'mongodb://localhost:27017/';
    }
    public CreateCollection(name:string){
        MongoClient.connect(this.url, function (err, db) {
            if (err) throw err;
            console.log('数据库已创建');
            var dbase = db.db("mongo");
            dbase.createCollection(name, function (err, res) {
                if (err) throw err;
                console.log("创建集合!");
                db.close();
            });
        });
    }
    public deleteMany(collection:string,query:object){
        MongoClient.connect(this.url, function(err, db) {
            if (err) throw err;
            var dbo = db.db("mongo");
            var whereStr = query;  // 查询条件
            dbo.collection(collection).deleteMany(whereStr, function(err, obj) {
                if (err) throw err;
                console.log(obj.result.n + " 条文档被删除");
                db.close();
            });
        });
    }
    public deleteOne(collection:string,query:object){
        MongoClient.connect(this.url, function(err, db) {
            if (err) throw err;
            var dbo = db.db("mongo");
            var whereStr = query;  // 查询条件
            dbo.collection(collection).deleteOne(whereStr, function(err, obj) {
                if (err) throw err;
                console.log("文档删除成功");
                db.close();
            });
        });
    }
    public find(collection:string,query:object,callback:any){
        MongoClient.connect(this.url,{useNewUrlParser:true}, function(err, db) {
            if (err) throw err;
            var dbo = db.db("mongo");
            var where  =  query
            dbo.collection(collection). find(where).toArray(function(err, result) { // 返回集合中所有数据
                if (err) throw err;
                callback(result);
                db.close();
            });
        });
    }
    public insert(collection:string,query:object){
        MongoClient.connect(this.url,function(err,db){
            if(err)throw err ;
            var dbo = db.db("mongo");
            var myobj = query;
            dbo.collection(collection).insertOne(myobj, function(err, res) {
                if (err) throw err;
                console.log("文档插入成功");
                db.close();
            });
        })
    }
    public insertMany(collection:string,query:any){
        MongoClient.connect(this.url, function(err, db) {
            if (err) throw err;
            var dbo = db.db("mongo");
            var myobj =  query;
            dbo.collection(collection).insertMany(myobj, function(err, res) {
                if (err) throw err;
                console.log("插入的文档数量为: " + res.insertedCount);
                db.close();
            });
        });
    }
    public SortCollection(collection:string,query:object){
        MongoClient.connect(this.url, function(err, db) {
            if (err) throw err;
            var dbo = db.db("mongo");
            var mysort = query;
            dbo.collection(collection).find().sort(mysort).toArray(function(err, result) {
                if (err) throw err;
                console.log(result);
                db.close();
            });
        });
    }
    public updateMany(collection:string,query:object,content:any){
        MongoClient.connect(this.url, function(err, db) {
            if (err) throw err;
            var dbo = db.db("mongo");//collections
            var whereStr = query;  // 查询条件
            var updateStr = {$set: content};//更新的地方
            dbo.collection(collection).updateMany(whereStr, updateStr, function(err, res) {
                if (err) throw err;
                console.log(res.result.nModified + " 条文档被更新");
                db.close();
            });
        });
    }
    public updateOne(col:string,que:object,content:any){
        MongoClient.connect(this.url, function(err, db) {
            if (err) throw err;
            var dbo = db.db("mongo");
            var whereStr = que;  // 查询条件
            var updateStr = {$set: content};
            dbo.collection(col).updateOne(whereStr, updateStr, function(err, res) {
                if (err) throw err;
                console.log("文档更新成功");
                db.close();
            });
        });
    }
}